﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AnimatedSprite
{
    public class SimpleAnimatedSprite
    {
        public readonly Texture2D Texture;

        public readonly TimeSpan FrameRate;
        private TimeSpan _timeElapsed;

        public readonly Vector2 FrameOffset;
        public readonly Vector2 FrameSize;

        public readonly int FrameCount;
        private int _currentFrame;
        private Rectangle _currentSourceRectangle;

        public Vector2 Position;
        public Vector2 Origin;

        public Color Tint;
        private bool _isAnimating;
        private bool _loop;

        private int _animationColumns;


        public bool IsAnimating { get { return _isAnimating; } }

        public int CurrentFrame
        {
            get { return _currentFrame; }
            private set
            {
                _currentFrame = value;

                int column = value%_animationColumns;
                int line = value/_animationColumns;

                _currentSourceRectangle = new Rectangle(
                    (int)(FrameOffset.X + (FrameSize.X * column)),
                    (int)(FrameOffset.Y + (FrameSize.Y * line)),
                    (int)FrameSize.X,
                    (int)FrameSize.Y);
            }
        }

        public Rectangle CurrentSourceRectangle
        {
            get { return _currentSourceRectangle; }
        }

        public SimpleAnimatedSprite(Texture2D texture, TimeSpan frameRate, Vector2 frameOffset, Vector2 frameSize, int frameCount)
            : this(texture, frameRate,frameOffset, frameSize, frameCount, 1)
        {
        }

        public SimpleAnimatedSprite(Texture2D texture, TimeSpan frameRate, Vector2 frameOffset, Vector2 frameSize, int frameCount, int spriteAnimationColumns)
        {
            Texture = texture;
            FrameCount = frameCount;
            FrameSize = frameSize;
            FrameOffset = frameOffset;
            FrameRate = frameRate;


            _animationColumns = spriteAnimationColumns;

            _isAnimating = false;
            CurrentFrame = 0;

            _timeElapsed = TimeSpan.Zero;

            Tint = Color.White;

            Origin = new Vector2();

        }


        public void PlayAnimation() { PlayAnimation(false); }
        public void PlayAnimation(bool loop)
        {
            _isAnimating = true;
            _loop = loop;
        }

        public void Update(GameTime gameTime)
        {
            if (IsAnimating)
            {
                _timeElapsed += gameTime.ElapsedGameTime;
                if (_timeElapsed.TotalMilliseconds > FrameRate.TotalMilliseconds)
                {

                    // Increment the current frame, wrapping back to 0 at iFrameCount
                    if(CurrentFrame  < FrameCount - 1)
                        CurrentFrame++;
                    else if (_loop)
                        CurrentFrame = 0;
                        
                    // Reset the elapsed frame time.
                    _timeElapsed = TimeSpan.Zero;
                }
            }

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, CurrentSourceRectangle, Color.White, 0f, Origin, Vector2.One, SpriteEffects.None, 1f);
        }
    }
}
